package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.utils.PinYin4jUtils;
import cn.itcast.bos.web.action.commen.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@Namespace("/")
@Scope("prototype")
@ParentPackage("struts-default")
public class AreaAction extends BaseAction<Area> {
    @Autowired
    private AreaService areaService;
    private File file;
    private String ids;

    public void setIds(String ids) {
        this.ids = ids;
    }

    public void setFile(File file) {
        this.file = file;
    }

    //新增
    @Action(value = "area_save",results = {@Result(name = "success",type = "redirect",location = "./pages/base/area.html")})
    public String save(){
        areaService.save(model);
        return SUCCESS;
    }
    //删除
    @Action(value = "area_delBatch",results = {@Result(name = "success",type = "redirect",location = "./pages/base/area.html")})
    public String delBatch(){
        String[]idsArray=ids.split("\\.");
        areaService.delBatch(idsArray);
        return SUCCESS;
    }

    //批量区域数据导入
    @Action(value = "area_batchImport")
    public String batchImport() throws IOException {
        List<Area> area = new ArrayList<>();
        String fileName=file.getName();
        String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
        // 编写解析代码逻辑
        // 基于.xls 格式解析 HSSF
        // 1、 加载Excel文件对象
        if("xls".equals(prefix)) {
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
            // 2、 读取一个sheet
            HSSFSheet sheet = hssfWorkbook.getSheetAt(0);

        /*//基于xlsx解析

        else if("xlsx".equals(prefix)) {
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
        }*/

        // 3、 读取sheet中每一行
        for (Row row : sheet
                ) {
            if (row.getRowNum() == 0) {
                continue;
            }
            if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
                break;
            }
            Area area1 = new Area();
            area1.setId(row.getCell(0).getStringCellValue());
            area1.setProvince(row.getCell(1).getStringCellValue());
            area1.setCity(row.getCell(2).getStringCellValue());
            area1.setDistrict(row.getCell(3).getStringCellValue());
            area1.setPostcode(row.getCell(4).getStringCellValue());
            // 基于pinyin4j生成城市编码和简码
            String province = area1.getProvince();
            String city = area1.getCity();
            String district = area1.getDistrict();
            province = province.substring(0, province.length() - 1);
            city = city.substring(0, city.length() - 1);
            district = district.substring(0, district.length() - 1);
            // 简码
            String[] headArray = PinYin4jUtils.getHeadByString(province + city + district);
            StringBuffer stringBuffer = new StringBuffer();
            for (String headStr : headArray
                    ) {
                stringBuffer.append(headStr);
            }
            String shortcode = stringBuffer.toString();
            area1.setShortcode(shortcode);
            // 城市编码
            String citycode = PinYin4jUtils.hanziToPinyin(city, "");
            area1.setCitycode(citycode);
            area.add(area1);
        }
        }
        areaService.saveBatch(area);

        return NONE;
    }

}
